<template>
  <div class="normalMaintenanceStandardDetails h100">
    <editPageNew
      ref="editPage"
      :rightBtnArr="rightBtnArr"
      showTopTitle
      :creatInfo="formData"
      :moreBtnArr="moreBtnArr"
      :showMore="pageType != 'add'"
      @closeBox="() => (showCheckRecord = false)"
    >
      <template slot="editPageTitle">
        <span class="title-code"> 巡视工单 {{ formData.code }}</span>
      </template>
      <template slot="statusText"> 当前状态：{{ statusName }} </template>
      <div class="plan-title">
        <div class="name">
          <span style="color: red"></span>
          <span>{{ titleName }}</span>
        </div>
      </div>
      <el-collapse v-model="activeNames" style="padding-right: 10px">
        <el-collapse-item name="1">
          <template slot="title">
            <span class="titleLine"></span>
            基本信息
          </template>
          <basicInfo
            :allInfo="formData"
            :status="status"
            @unTimeRelease="unTimeRelease"
            ref="basicInfo"
          />
        </el-collapse-item>
        <el-collapse-item name="2">
          <template slot="title">
            <span class="titleLine"></span>
            巡视情况
          </template>
          <patrolInfo
            :orderId="orderId"
            ref="patrolInfo"
            @setPointList="setPointList"
          />
        </el-collapse-item>
        <el-collapse-item name="3" v-if="isShowTimer">
          <template slot="title">
            <span class="titleLine"></span>
            报缺记录
          </template>
          <defectRecord :orderId="orderId" />
        </el-collapse-item>
      </el-collapse>
      <template slot="boxContent">
        <checkRecord
          v-if="showCheckRecord"
          :orderData="formData"
          ref="checkRecord"
        ></checkRecord>
      </template>
    </editPageNew>
    <dialogPublic
      title="状态维护"
      :show.sync="show"
      v-if="show"
      width="30%"
      @doSubmit="doSumbit"
      @doClose="() => (show = false)"
    >
      <div style="padding: 10px 40px 10px 0">
        <editForm
          :formConfig="basicConfig"
          ref="editForm"
          :formData="formData"
        ></editForm>
      </div>
    </dialogPublic>
  </div>
</template>
<script>
import {
  getBasicInfo,
  updatePatrolOrder,
  downloadPatrolCheck,
} from "@/api/orderManagement/patrolOrder.js";
import { getFilesList } from "@/api/common.js";
import checkRecord from "../checkRecord";
import basicInfo from "./basicInfo.vue";
import patrolInfo from "./patrolInfo.vue";
import defectRecord from "./defectRecord.vue";
export default {
  components: {
    checkRecord,
    basicInfo,
    patrolInfo,
    defectRecord,
  },
  data() {
    return {
      activeNames: ["1", "2", "3"],
      titleName: "",
      isShowTimer: true, //是否展示定时器信息
      hideBtn: false,
      status: -1,
      show: false,
      showCheckRecord: false,
      orderId: "",
      basicConfig: {
        labelWidth: "100px",
        itemArr: [
          {
            label: "状态",
            prop: "status",
            type: "select",
            span: 24,
            required: true,
            disabled: false,
            selectOption: this.$store.getters.dirtData("order_patrol_status"),
          },
          {
            label: "备注:",
            prop: "remarks",
            type: "textarea",
            required: true,
            disabled: false,
            rows: 4,
            span: 24,
          },
        ],
      },
      formData: {},
      checkItemList: [],
      paramsItemList: [],
      pointList: [],
    };
  },
  computed: {
    rightBtnArr() {
      let arr = [
        {
          text: "状态管理",
          icon: "el-icon-check",
          hide:
            ["details", "perform"].includes(this.pageType) ||
            this.status == "END_PATROL",
          methods: () => {
            this.show = true;
          },
        },
      ];
      return arr;
    },
    moreBtnArr() {
      let arr = [
        {
          text: "工单抽查",
          hide: this.status != "END_PATROL",
          methods: () => {
            this.$refs.editPage.showEditPageBox();
            this.showCheckRecord = true;
          },
        },
        {
          text: "操作记录",
          methods: () => {
            this.$refs.editPage.showOperate();
          },
        },
        {
          text: "导出巡视点",
          methods: () => {
            this.exportPointList();
          },
        },
        {
          text: "批量下载巡点附件",
          methods: () => {
            this.debounceMethods(this.downLoadFile);
          },
        },
      ];
      return arr;
    },
    unChangeable() {
      return this.status == 1 || this.pageType == "detail";
    },
    unEdit() {
      return ["query", "detail"].includes(this.pageType) || this.status == 1;
    },
    statusName() {
      return this.$store.getters.fineDirtDataName(
        "order_patrol_status",
        this.status
      );
    },
  },
  created() {
    this.init();
  },
  mounted() {},
  methods: {
    init() {
      let { id, pageType, status, insertFlag } = this.$route.query;
      this.pageType = pageType;
      this.status = status;
      this.orderId = id;
      if (pageType != "add") {
        // if (!insertFlag) {
        //   this.getDetails(id);
        // } else {
        this.getDetailsAfterInsert(id);
        // }
      } else {
        this.showInput = true;
      }
      if (["query", "detail"].includes(pageType)) {
        this.basicConfig.itemArr.map((i) => (i.disabled = true));
        this.hideBtn = true;
      }
    },
    //是否定时下达
    unTimeRelease(val) {
      this.isShowTimer = !val;
    },
    //获取详情初始化巡视点数据
    getDetailsAfterInsert(id) {
      getBasicInfo({ orderId: id }).then((res) => {
        if (res.data) {
          this.$refs.patrolInfo.getPointList();
          this.formData = res.data;
          this.titleName = this.formData.title;
          this.status = this.formData.status;
          if (this.status) {
            this.hideBtn = true;
            this.titleEdit = false;
          } else {
            this.hideBtn = false;
            this.titleEdit = true;
          }
        } else {
          this.getDetails(id);
        }
      });
    },
    //获取标准详情
    getDetails(id) {
      getBasicInfo({ orderId: id }).then((res) => {
        if (res.code == 200) {
          this.$refs.patrolInfo.getPointList();
          this.formData = res.data;
          this.titleName = this.formData.title;
          this.status = this.formData.status;
          if (this.status) {
            this.hideBtn = true;
            this.titleEdit = false;
          } else {
            this.hideBtn = false;
            this.titleEdit = true;
          }
        }
      });
    },
    //设置所有巡视点数据
    setPointList(data) {
      this.pointList = data;
    },
    async downLoadFile() {
      if (this.pointList.length == 0) {
        this.$message.warning("未检测到巡视点信息，无法下载");
      } else {
        await downloadPatrolCheck({ orderId: this.formData.id });
        let url = `eam-service-order/patrol/downloadPatrolZip?orderId=${this.formData.id}&zipName=${this.titleName}`;
        let fileName = `${this.titleName}.zip`;
        this.$utils.downloadFile(null, fileName, url);
      }
    },
    //导出所有巡视点
    exportPointList() {
      let params = {
        ids: [this.formData.id],
      };
      let url = `eam-service-order/patrol/export`;
      let fileName = `${this.titleName}巡视点`;
      this.$utils.downloadFilePost(null, fileName, url, params);
    },
    //提交状态维护
    async doSumbit() {
      try {
        let params = await this.$refs.editForm.getFormData();
        let res = await updatePatrolOrder(params);
        this.$message.success(res.description);
        this.$utils.addOperateLog(this.formData.id, `修改了巡视工单状态`);
        this.show = false;
        this.getDetails(this.formData.id);
        this.updateListData();
      } catch (error) {
        console.log(error);
      }
    },
    /* 更新列表页的数据 */
    updateListData() {
      /* 通过localStorage来实现浏览器不同窗口之间的通信 */
      let num = Math.floor(Math.random() * 1000) + 1; //产生随机数，保证每次的值都不一样
      localStorage.setItem("update", num); //设置localStorage里面的值，保证每次都会触发storage事件
    },
  },
};
</script>
<style lang="less" scoped>
.normalMaintenanceStandardDetails {
  // overflow: hidden;
  background-color: #ffffff;
  .title {
    font-size: 12px;
    font-weight: 800;
    color: #666666;
    background: #f4f4f4;
    padding: 4px 10px;
    border-radius: 14px;
  }
  .TitleImg {
    margin-right: 2px;
    font-size: 22px;
  }
  .plan-title {
    height: 40px;
    margin: 0 0 10px 0;
    .name {
      font-size: 18px;
      padding: 0 10px;
      height: 40px;
      font-weight: bold;
      line-height: 40px;
      text-align: left;
      padding: 0 10px;
    }
    .name-input {
      width: 400px;
      height: 40px;
      line-height: 40px;
    }
  }
}
</style>
